package com.zd.commodity.userMng.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import utils.MD5Util;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.userMng.biz.IRoleBiz;
import com.zd.commodity.userMng.biz.IUserBiz;
import com.zd.commodity.userMng.entity.Role;
import com.zd.commodity.userMng.entity.User;

public class LoginAction extends BaseAction<User>{
	private IUserBiz userBiz;
	private String username;
	private String password;
	private IRoleBiz roleBiz;
	
	

	//退出
	public void logout(){
		request.getSession().removeAttribute("user");
	}
	
	//登陆验证
	public void checkUser(){
		String md5Pswd = MD5Util.MD5(username+password);
		User user = userBiz.findByUsernameAndPwd(username, md5Pswd);
		if(user==null){
			write(ajaxReturn(false,"用户名或密码错误！",null));
		}else{
			String remoteAddr = request.getRemoteAddr();
			userBiz.updateLoginInfo(new Timestamp(System.currentTimeMillis()), remoteAddr, user.getId());
			request.getSession().setAttribute("user", user);
			write(ajaxReturn(true,"登陆成功！",null));
		}
	}
	
	
	//为了增加用户时选择角色
	@Override
	public String toAdd(){
		
		String params=super.getParams();
		super.request.setAttribute("params",params );
		List<Role> roles=roleBiz.getList(null, null,params);
		ActionContext.getContext().put("roles", roles);
		return "toAdd";
	}
	
	
	@Override
	public String get()
	{		
		List<Role> roles=roleBiz.getList(null, null,null);
		ActionContext.getContext().put("roles", roles);
		
		User user=userBiz.get(super.getId());
		request.setAttribute("params", super.getParams());
		ActionContext.getContext().put("t", user);
		ActionContext.getContext().put("method", "update");
		return "form";
	}
	
	
	//获取当前用户信息  为了更改当前用户的密码
	public String toUpdPsw(){
		request.setAttribute("params", super.getParams());
		User user=(User)request.getSession().getAttribute("user");
		ActionContext.getContext().put("t", user);
		return "toUpdPsw";
	}
	
	//修改当前用户密码
	public void updSelfPwd(){
		User user=super.getT();
		String md5Pswd = MD5Util.MD5(user.getUsername()+user.getPassword());
		user.setPassword(md5Pswd);
		userBiz.update(user);	
	}
	
	
	//修改用户密码
	public String updUserPwd(){
		request.setAttribute("page", super.getPage());
		
		User user=super.getT();
		String md5Pswd = MD5Util.MD5(user.getUsername()+user.getPassword());
		System.out.println(md5Pswd);
		user.setPassword(md5Pswd);
		userBiz.update(user);
		return super.listByPage();
	}
	
	
	
	//判断用户名是否已存在
	public void nameExist() throws IOException{
		String username=super.getT().getUsername();
		User user=userBiz.findByUsername(username);
		JSONObject json=new JSONObject();
		
		if(user!=null){
			json.put("valid", false);
		}else{
			json.put("valid", true);
		}
		response.getWriter().write(json.toString());
	}
	
	//判断当前用户原密码是否正确
	public void checkpwd() throws IOException{
		User user=(User)request.getSession().getAttribute("user");
		
		String md5Pswd = MD5Util.MD5(user.getUsername()+request.getParameter("oldpwd"));
		
		JSONObject json=new JSONObject();
		if(user.getPassword().equals(md5Pswd)){
			json.put("valid", true);
		}else{
			json.put("valid", false);
		}
		response.getWriter().write(json.toString());
	}
			
	public IUserBiz getUserBiz() {
		return userBiz;
	}
	public void setUserBiz(IUserBiz userBiz) {
		this.userBiz = userBiz;
		setBaseBiz(userBiz);
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public IRoleBiz getRoleBiz() {
		return roleBiz;
	}
	public void setRoleBiz(IRoleBiz roleBiz) {
		this.roleBiz = roleBiz;
	}
}
